#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include "AppFlickRView.h"
#include <UIEngine.h>
#include <UIWindow.h>
#include <UIPNGImage.h>
#include <UIJPGImage.h>
#include <UIText.h>

#include "FlickR_Img.h"
#include "AppFlickRModel.h"


AppFlickRView::AppFlickRView() :_loaded(false),clicked(false){
  _model.loadImgFromId("25211807@N07");
  _pictures = _model.cacheFiles(25);
  _largePictures = _model.cacheLargeFiles(25);
  _ui = new UIEngine(RGBAColor(0.1, 0.1, 0.1, 1.), RGBAColor(0., 0., 0., 1.), 2.
		     ,2., 0., 0.);
}

void AppFlickRView::displayFront() {
  commonGL();

  // Draw select zone
  float posX_sel = 0.9;
  float posY_sel = 0.35;
  posX_sel -= 0.37*(float)(_xsel);
  posY_sel += 0.37*(float)(_ysel);
  glBegin(GL_QUADS);
    glColor3ub(255, 255, 255);
    glVertex3d(posX_sel+0.03, posY_sel+0.03, -0.01);
    glVertex3d(posX_sel+0.03, posY_sel-0.33, -0.01);
    glVertex3d(posX_sel-0.33, posY_sel-0.33, -0.01);
    glVertex3d(posX_sel-0.33, posY_sel+0.03, -0.01);
  glEnd();

  // Zoom on an image
  if(clicked){
    GLuint _tex;
    std::string * file;
    int i = 1;
    
    for(std::list<std::string>::iterator it = _largePictures.begin();
	it != _largePictures.end();
	it++){
      if(i == (5-_ysel-1)*(5) + (_xsel+1)){
	file = new std::string(*it);
	break;
      }
      ++i;
    }
    std::cout << file << std::endl;
    fflush(stdout);

    SDL_Surface* image = IMG_Load(file->c_str());
    if (!image) {
      std::cout << "IMG_Load: " << IMG_GetError() << std::endl;
    } else {
      glGenTextures(1, &_tex);
      glBindTexture(GL_TEXTURE_2D, _tex);
      glColor3f(1.0,1.0,1.0);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

      SDL_LockSurface(image);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image->w, image->h, 0, GL_RGB,
		   GL_UNSIGNED_BYTE, image->pixels);
      SDL_UnlockSurface(image);

      SDL_FreeSurface(image);
    }

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, _tex);

    glBegin(GL_QUADS);
    glColor3f(1.0,1.0,1.0);
    glNormal3f(0.0, 0.0, 1.0);

    glTexCoord2d(-1, 0);
    glVertex3f(-1.0, 0.0, -1.0);
    glTexCoord2d(-1, -1);
    glVertex3f(-1.0, 2., -1.0);
    glTexCoord2d(0, -1);
    glVertex3f(1.0, 2., -1.0);
    glTexCoord2d(0, 0);
    glVertex3f(1.0, 0.0, -1.0);
    glEnd();
  }

}

void AppFlickRView::displayBack() {
  commonGL();
}

void AppFlickRView::transitive(float percent) {
  commonGL();
}

void AppFlickRView::setSelected(int x, int y){
  _xsel = x;
  _ysel = y;
 
}

void AppFlickRView::setClicked(int x, int y){
  _xsel = x;
  _ysel = y;
  clicked = !clicked;
}

AppFlickRView::~AppFlickRView() {
  delete _ui;
}

void AppFlickRView::commonGL(){
   if(!_loaded){
    float posX = 0.6;
    float posY = 1.6;
    _ui->addUIComponent(new UIWindow(-1., 0., "App FlickR"));
    for(std::list<std::string>::iterator it = _pictures.begin();
	it != _pictures.end();
	it++){
      _ui->addUIComponent(new UIJPGImage(posX, posY, 0.3, 0.3,*it));
      
      posX-=0.37;
      if(posX < -0.9){
	posX = 0.6;
	posY-=0.37;
      }
    }
    _loaded = true;
  }
 
  _ui->display();
}
